<?php
namespace Admin\Controller;
use Think\Controller;
class CompanyInfoController extends AdminController{
    /**
     * 公司信息设置
     */
    public function index()
    {
        if (IS_POST) {
            $rules = array(
                array('name', 'require', '公司名称是必须的'),
                array('address', 'require', '公司地址是必须的'),
                array("mobile_free", '/^[0-9-()（）]{7,18}$/', '请填写正确的免费电话'),
                array('mobile_market', '/^[0-9-()（）]{7,18}$/', '请填写正确的市场部电话'),
                array('mobile_sell', '/^(([0\+]\d{2,3}-)?(0\d{2,3})-)?(\d{7,8})(-(\d{3,}))?$/ ', '请填写正确的销售部电话'),
                array('mobile_world', '/^[0-9-()（）]{7,18}$/', '请填写正确国际贸易部的电话'),
                array('fax', ' /^(\d{3,4}-)?\d{7,8}$/', '请填写正确的传真'),
                array('email', '/^\w[-\w.+]*@([A-Za-z0-9][-A-Za-z0-9]+\.)+[A-Za-z]{2,14}$/', '请填写正确的邮箱'),
                array('postcode', '/^\d{6}$/', '请填写正确的邮编'),
                array('img','require','公司图片是必须的'),
            );
            $CompanyInfo = M("CompanyInfo"); // 实例化User对象
            if (!$CompanyInfo->validate($rules)->create()) {
                $this->error($CompanyInfo->getError());
            } else {
                $data = I("post.");
                $data['c_time'] = time();
                $data['u_time'] = time();
                if ($CompanyInfo->data($data)->add()) {
                    $this->success('公司信息设置成功', U('index'));
                } else {
                    $this->error('公司信息设置失败');
                }
            }
        } else {
            $one = M('CompanyInfo')
                ->order('u_time desc')
                ->limit(0, 1)
                ->find();
            $this->assign('one', $one);
            $this->assign('title', '公司信息设置');
            $this->display();
        }
    }

    /**
     * 水印设置
     */
    public function water_mark(){
        if (IS_POST){
            $rules = array(
                array('mark_img','not_empty','水印图片不得为空',1,'function'),
            );

            $Water = M("Water"); // 实例化User对象
            if (!$Water->validate($rules)->create()) {
                $this->error($Water->getError());
            } else {
                $data = I("post.");
                if(I('post.alpha')== ''){
                    $data['alpha'] = 80;
                }
                if ($Water->data($data)->add()) {
                    $this->success('水印设置成功', U());
                } else {
                    $this->error('水印设置失败');
                }
            }
        }else{
            $one = M('water')
                ->order('id desc')
                ->limit(0, 1)
                ->find();
            $this->assign('one', $one);
            $this->assign('title', '水印设置');
            $this->display();
        }
    }

    public function upload_img($type=0){
        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize = 2 * 1024 * 1024 ;// 设置附件上传大小 2m
        $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath = './Uploads/'; // 设置附件上传根目录
        // $upload->savePath = 'Goods/'; // 设置附件上传（子）目录
        $upload->subName = array('date','Y/m/d');
        // 上传文件
        $info = $upload->uploadOne($_FILES['file']);
//        dump($info);
//        exit;
        if(!$info) { // 上传错误提示错误信息
            $this->ajaxReturn(array("error"=>$upload->getError()));
        }else{ // 上传成功
            $pic_url = 'Uploads/'.$info['savepath'].$info['savename'];
            $image = new \Think\Image();
            $img = $image->open($pic_url);
            if($type == 1){
                $img->thumb(666,422,3)->save($pic_url);
            }else{
                $img->thumb(80, 39,3)->save($pic_url);
            }
            $this->ajaxReturn(array('error'=>'0','pic'=>__ROOT__.'/'.$pic_url,'name'=>$info['savename'],'width'=>'80px'));
        }
    }
}